package com.helsinki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.helsinki.mapper.PermissionMapper;
import com.helsinki.mapper.ProductMapper;
import com.helsinki.pojo.Permission;
import com.helsinki.pojo.Product;
import com.helsinki.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> findAll() {
        return permissionMapper.findAll();
    }

    @Override
    public PageInfo<Permission> queryAll(Integer pageNum, Integer pageSize) {
        // 只有在要分页的查询之前才能设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Permission> list = permissionMapper.findAll();
        // PageInfo 封装了所有的分页信息
        return new PageInfo<>(list);
    }

    @Override
    public void addPermission(Permission permission) {
        UUID uuid = UUID.randomUUID();
        permission.setId(uuid.toString().replaceAll("\\-", ""));

        permissionMapper.addPermission(permission);
    }

    @Override
    public void updatePermission(Permission permission) {
        permissionMapper.updateById(permission);
    }

    @Override
    public Permission queryById(String id) {
        return permissionMapper.queryById(id);
    }

    @Override
    public Boolean deleteById(String id) {
        List<String> rid = permissionMapper.findRoleByPId(id);
        if(rid.isEmpty()){
            permissionMapper.closeForeignKey();
            permissionMapper.deleteById(id);
            permissionMapper.openForeignKey();
            return true;
        }else {
            return false;
        }
    }
}
